package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.DietFileEntity;

public class DietFileDAO {
	public boolean insert(Connection conn, DietFileEntity dietFileEntity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement("insert into diet_file(diet_patient_id,filename,type) values(?,?,?)");
		ps.setInt(1, dietFileEntity.getDiet_patient_id());
		ps.setString(2, dietFileEntity.getFilename());
		ps.setString(3, dietFileEntity.getType());
		b=ps.executeUpdate()>0?true:false;
		return b;
	}

	public List<DietFileEntity> find(Connection conn, int pid, String type) throws SQLException {
		List<DietFileEntity> list = new LinkedList<DietFileEntity>();
		PreparedStatement ps = conn.prepareStatement("select * from diet_file where type=? and diet_patient_id=?");
		ps.setString(1, type);
		ps.setInt(2, pid);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietFileEntity dietFileEntity = new DietFileEntity();
			dietFileEntity.setDiet_patient_id(rs.getInt("diet_patient_id"));
			dietFileEntity.setFilename(rs.getString("filename"));
			dietFileEntity.setType(rs.getString("type"));
			list.add(dietFileEntity);
		}
		return list;
	}

	public List<DietFileEntity> find(Connection conn, int id) throws SQLException {
		List<DietFileEntity> list = new LinkedList<DietFileEntity>();
		PreparedStatement ps = conn.prepareStatement("select * from diet_file where diet_patient_id=?");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietFileEntity dietFileEntity = new DietFileEntity();
			dietFileEntity.setDiet_patient_id(rs.getInt("diet_patient_id"));
			dietFileEntity.setFilename(rs.getString("filename"));
			dietFileEntity.setType(rs.getString("type"));
			list.add(dietFileEntity);
		}
		return list;
	}

	public void deleteBydpid(Connection connection, int diet_patient_id) throws SQLException {
		PreparedStatement ps=connection.prepareStatement("delete from diet_file where diet_patient_id=?");
		ps.setInt(1, diet_patient_id);
		ps.executeUpdate();
	}

	public List<DietFileEntity> listBydpid(Connection connection, int dpid) throws SQLException {
		List<DietFileEntity> list = new LinkedList<DietFileEntity>();
		PreparedStatement ps = connection.prepareStatement("select * from diet_file where diet_patient_id=?");
		ps.setInt(1, dpid);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietFileEntity dietFileEntity = new DietFileEntity();
			dietFileEntity.setDiet_patient_id(rs.getInt("diet_patient_id"));
			dietFileEntity.setFilename(rs.getString("filename"));
			dietFileEntity.setType(rs.getString("type"));
			list.add(dietFileEntity);
		}
		return list;
	}
}
